package com.suray.wcs.service.pojo.sendmap;

import com.suray.basic.wcs.utils.LoggerUtil;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.wcs.res.enums.RgvStatus;
import com.suray.wcs.service.pojo.Rgv;
import com.suray.wcs.service.pojo.rgv.rgv.protocol.base.RgvBaseMessage;
import com.suray.wcs.service.pojo.rgv.rgv.protocol.message.RgvFileRecMessage;
import com.suray.wcs.service.pojo.rgv.rgv.protocol.message.RgvFileSendMessage;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class RgvFileSendSector {

    public static final int FILE_MESSAGE_DATA_SIZE = 1024;

    static class FileData {
        public byte[] data = new byte[FILE_MESSAGE_DATA_SIZE];

        public FileData() {
            for (int i = 0; i < data.length; i++) {
                data[i] = 0x1A;
            }
        }

        public FileData(String mapName) {
            for (int i = 0; i < mapName.length(); i++) {
                data[i] = (byte) mapName.charAt(i);
            }
        }
    }

    public static void sendFile(InputStream fileInputStream, Rgv rgv, String fileName) throws IOException {
        List<FileData> fileDataList = new ArrayList<>();
        DataInputStream inputStream = new DataInputStream(fileInputStream);
        fileDataList.add(new FileData(fileName));
        FileData fileData = new FileData();
        while (inputStream.read(fileData.data) != -1) {
            fileDataList.add(fileData);
            fileData = new FileData();
        }
        LoggerUtil.warn("文件发送开始总共" + fileDataList.size() + "包", RgvFileSendMessage.class);
        try {
            for (int i = 0; i < fileDataList.size(); i++) {
                sendFileData(rgv, fileDataList, i);
            }
        } catch (Exception e) {
            WcsFuncUtil.exceptionLog(e, RgvFileSendSector.class);
            rgv.setRgvStatus(RgvStatus.ERROR);
            rgv.setErrorMessage("传输文件失败");
        }
    }

    private static void sendFileData(Rgv rgv, List<FileData> mapDataList, int index) throws IOException {
        RgvBaseMessage baseMessage
                = rgv.getRgvProtocol().comm(
                new RgvFileSendMessage(rgv, mapDataList.size() - 1, index, mapDataList.get(index).data));

        if (baseMessage instanceof RgvFileRecMessage) {
            RgvFileRecMessage fileRecMessage = (RgvFileRecMessage) baseMessage;

            int result = fileRecMessage.getResult();
            if (result == 0x15) {
                sendFileData(rgv, mapDataList, index);
            } else if (result != 0x6 && result != 0x7) {
                throw new RuntimeException("地图发送时收到的返回错误：" + result);
            }
        } else {
            throw new RuntimeException("接收到的数据类型错误,当前数据为地图发送-->" + baseMessage.getMessageType());
        }
    }

    private static void testFile(String filePath, List<FileData> mapDataList) {
        filePath += "1";

        try {
            DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(filePath));
            for (int i = 1; i < mapDataList.size(); i++) {
                outputStream.write(mapDataList.get(i).data);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
